import {
	threadInfo,
} from '@/network/api.js'
/** 平台模块 */
export default function useThreadInfo() {
	const getThreadInfo = () => {
		return new Promise(async (resolve, reject) => {
			const res = await threadInfo(1)
			if (res.status === 200) {
				const info = res.data
				const votesInfo = res.data.votes[0]
				const endTime = votesInfo?.end_time
				let day = 0, hour = 0, minute = 0, second = 0
				if (endTime) {
					const currentTimeStamp = Date.now();
					const targetTime = new Date(endTime);
					if (targetTime > currentTimeStamp) {
						const timeDiff = targetTime - currentTimeStamp;
						day = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
						hour = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
						minute = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
						second = Math.floor((timeDiff % (1000 * 60)) / 1000);
					}

				}
				resolve({
					info,
					votesInfo,
					time: {
						day, hour, minute, second
					}
				})
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				reject()
			}
		})
	}

	return { getThreadInfo }
}